package com.company.project.service.impl.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.project.command.warehouse.WareHouseListCommand;
import com.company.project.command.warehouse.WarehouseCreateorUpdateCommand;
import com.company.project.ctype.WarehouseState;
import com.company.project.entity.warehouse.Warehouse;
import com.company.project.exception.NormalException;
import com.company.project.mapper.WareHouseMapper;
import com.company.project.service.HttpSessionService;
import com.company.project.service.warehouse.WareHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.List;
import java.util.Objects;

@Service
public class WareHouseServiceImpl extends ServiceImpl<WareHouseMapper, Warehouse> implements WareHouseService {

    @Autowired
    private WareHouseMapper wareHouseMapper;
    @Autowired
    private HttpSessionService httpSessionService;

    @Override
    public IPage<Warehouse> selectAllTables(WareHouseListCommand command) {
        Page pageVO = new Page(command.getPage(), command.getlimit());
        LambdaQueryWrapper<Warehouse> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(command.getOwner())) {
            queryWrapper.like(Warehouse::getOwner, command.getOwner());
        }
        if (!StringUtils.isEmpty(command.getName())) {
            queryWrapper.like(Warehouse::getName, command.getName());
        }
        if (Objects.nonNull(command.getCategory())) {
            queryWrapper.eq(Warehouse::getCategory, command.getCategory());
        }

        queryWrapper.orderByDesc(Warehouse::getCreatedDate);
        IPage<Warehouse> ipage = wareHouseMapper.selectPage(pageVO, queryWrapper);

        return ipage;
    }

    @Override
    public void create(WarehouseCreateorUpdateCommand command) {
        LambdaQueryWrapper<Warehouse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Warehouse::getName, command.getName());
        if (this.wareHouseMapper.selectOne(queryWrapper) != null) {
            throw new NormalException("仓库已存在，请勿重复创建");
        }
        Warehouse warehouse = new Warehouse();
        // 设置归属方
        warehouse.setOwner(command.getOwner());
        warehouse.setName(command.getName());
        warehouse.setCategory(command.getCategory());
        warehouse.setCreatedDate(Calendar.getInstance().getTime());
        warehouse.setCreatedBy(httpSessionService.getCurrentUsername());
        warehouse.setCurrentState(WarehouseState.Created.name());
        this.wareHouseMapper.insert(warehouse);

    }

    @Override
    public void update(String id, WarehouseCreateorUpdateCommand command) {
        Warehouse warehouse = this.wareHouseMapper.selectById(id);
        if (warehouse == null) {
            throw new NormalException("供应商不存在，操作失败");
        }

        warehouse.setOwner(command.getOwner());
        warehouse.setName(command.getName());
        warehouse.setCategory(command.getCategory());
        warehouse.setCreatedDate(Calendar.getInstance().getTime());
        warehouse.setCreatedBy(httpSessionService.getCurrentUsername());
        warehouse.setLastModifiedDate(warehouse.getCreatedDate());
        this.wareHouseMapper.updateById(warehouse);


    }

    @Override
    public void enable(String id, String CreatedBy) {
        Warehouse warehouse = this.wareHouseMapper.selectById(id);
        if(warehouse == null){
            throw new NormalException("供应商不存在，请重新选择");
        }
        warehouse.setCurrentState(WarehouseState.Enabled.name());
        this.wareHouseMapper.updateById(warehouse);

    }

    /**
     * 下拉选操作
     * @return
     */
    @Override
    public List<Warehouse> getOption() {
        LambdaQueryWrapper<Warehouse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Warehouse::getCurrentState, "Enabled");
        return this.wareHouseMapper.selectList(queryWrapper);
    }


    @Override
    public void disable(String id, String CreatedBy) {
        Warehouse warehouse = this.wareHouseMapper.selectById(id);
        if(warehouse == null){
            throw new NormalException("供应商不存在，请重新选择");
        }
        warehouse.setCurrentState(WarehouseState.Disabled.name());
        this.wareHouseMapper.updateById(warehouse);

    }


}
